<template>
	<view class="my-qr">
		<uni-popup type="center" ref="showPopup">
			<view style="background-color: #FFF; padding: 30rpx; border-radius: 8rpx;">
				<view class="authorize-tip">生成二维码成功</view>
				<view class="b-line"></view>
				<view class="s-line"></view>
				<image :src="img" mode=""></image>
				<view class="authorize-btn" @click="save">
					保存到相册
				</view>
			</view>
		</uni-popup>
		<pt-openSetting ref="openSetting" :tips="tips"></pt-openSetting>
	</view>
</template>

<script>
	export default {
		props: {
			img: {
				type: String,
				default: ''
			},
		},
		data() {
			return {
				tips: '您已关闭相册访问开关，请打开开关后重新保存',
				showPopup: false
			};
		},
		methods: {
			save() {
				// #ifdef APP-PLUS || H5
				uni.downloadFile({
					url: this.img,
					success: (downRes) => {
						uni.saveImageToPhotosAlbum({
							filePath: downRes.tempFilePath,
							success: (saveRes) => {
								uni.showToast({
									icon: 'none',
									title: '保存成功'
								})
							},
							fail: (err) => {
								uni.showToast({
									icon: 'none',
									title: '保存失败，您已取消保存'
								})
							}
						})
					}
				})
				// #endif
				// #ifdef MP-WEIXIN
				uni.getSetting({
					withSubscriptions: true,
					success: (scope) => {
						if(!scope.authSetting['scope.writePhotosAlbum']){
							uni.authorize({
								scope: 'scope.writePhotosAlbum',
								success: () => {
									uni.downloadFile({
										url: this.img,
										success: (downRes) => {
											uni.saveImageToPhotosAlbum({
												filePath: downRes.tempFilePath,
												success: (saveRes) => {
													uni.showToast({
														icon: 'none',
														title: '保存成功'
													})
												},
												fail: (err) => {
													uni.showToast({
														icon: 'none',
														title: '保存失败，您已取消保存'
													})
												}
											})
										}
									})
								},
								fail: () => {
									// uni.showToast({
									// 	icon: 'none',
									// 	title: '您已关闭相册访问开关，请开启后重新保存'
									// })
									this.$refs.openSetting.init()
								}
							})
						}else{
							uni.downloadFile({
								url: this.img,
								success: (downRes) => {
									uni.saveImageToPhotosAlbum({
										filePath: downRes.tempFilePath,
										success: (saveRes) => {
											uni.showToast({
												icon: 'none',
												title: '保存成功'
											})
										},
										fail: (err) => {
											uni.showToast({
												icon: 'none',
												title: '保存失败，您已取消保存'
											})
										}
									})
								}
							})
						}
					}
				})
				// #endif
			},
			open() {
				this.$refs.showPopup.open()
			}
		}
	}
</script>

<style lang="scss">
	.my-qr {

		.authorize-tip {
			padding: 40rpx 0 30rpx 0;
			font-size: 36rpx;
			text-align: center;
			font-weight: bold;
			color: #141414;
			line-height: 46rpx;
		}

		.b-line {
			margin: 0 auto;
			width: 340rpx;
			height: 4rpx;
			background: #141414;
		}

		.s-line {
			margin: 0 auto;
			margin-top: 8rpx;
			width: 340rpx;
			height: 2rpx;
			background: #444444;
		}

		.authorize-btn {
			margin: 0 auto;
			width: 520rpx;
			line-height: 88rpx;
			border-radius: 8rpx;
			font-size: 32rpx;
			text-align: center;
			background-color: #141414;
			color: #fff;
		}

		image {
			display: block;
			margin: 65rpx auto 74rpx auto;
			width: 292rpx;
			height: 292rpx;
		}

	}
</style>
